#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
  printf("I am a parent process,pid:%d,ppid:%d\n",getpid(),getppid());
  
  pid_t id = fork();
  if(id < 0)
  {
    exit(1);
  }

  else if(id == 0)
  {
    int cnt = 5;
    while(cnt--)
    {
      printf("I am a child process,pid:%d,ppid:%d\n",getpid(),getppid());
      sleep(1);
    }
    exit(10);
  }

  else{
    int status = 0;
    pid_t rid = waitpid(id, &status, 0 );
    if(rid > 0)
    {
     if(WIFEXITED(status))
     {

      printf("wait success,退出的子进程是:%d,exit_code:%d\n",rid,WEXITSTATUS(status));
     }
     else{

      printf("异常退出");
     }
    }
    else{
      printf("ret:%d\n",rid);
      perror("waitpid");
    }
  }

  return 0;
}
